import React, { Component, ComponentClass } from 'react';
import Taro from '@tarojs/taro';
import {View} from '@tarojs/components'
import {Good} from '../../utils/server.interface'
import BigGoodsBox from '../../components/big-goods-box/big-goods-box'
import {newHttp} from "../../utils/request";
import {IDENTITY_IS_SELLER, TO_USElIST} from "../../constants/api";
import {vcmonitor} from "../../utils/tools";
import {platform} from "../../utils/platform-settting.service";
import GzhAuth from "../../components/gzh-auth/gzh-auth"
import {http} from "../../utils/http";
import '../../common/search/search.scss'

interface stateType {
  list: Good[]
  isUser: boolean
}


type PageStateProps = {}

type PageDispatchProps = {}

type PageOwnProps = {}

type PageState = {}

type IProps = PageStateProps & PageDispatchProps & PageOwnProps

interface CouponGoods {
  props: IProps
}

class CouponGoods extends Component {
  state: stateType = {
    list: [],
    isUser: true
  }

  async UNSAFE_componentWillMount() {
    Taro.setNavigationBarTitle({
      title: platform.info.platformName
    })
    this.getList(this.page)
  }

  async componentDidShow() {
    vcmonitor(this)
    let isUser = true
    if (process.env.DISTINGUISH_IDENTITY) {
      const identityIsSeller = await http.get(IDENTITY_IS_SELLER, {})
      isUser = !identityIsSeller.isSeller
    }
    this.setState({isUser})
  }

  async onReachBottom() {
    if (this.canMore) {
      this.page++
      await this.getList(this.page)
    }
  }

  page = 1
  canMore = true

  async getList(p) {
    Taro.showLoading({title: '加载中...'})
    const coupon_issue_id = Taro.getCurrentInstance().router.params.id
    const ret = await newHttp.catchPost(TO_USElIST, {coupon_issue_id, p, s: 10, k: ''});
    Taro.hideLoading()
    if (ret && ret.length > 0) {
      if (p === 1) {
        this.setState({list: ret})
      } else if (p > 1) {
        const oldData = this.state.list;
        this.setState({list: [...oldData, ...ret]})
      }
    } else {
      this.canMore = false
      Taro.showToast({
        title: '没有更多数据了',
        icon: 'none'
      })
    }
  }


  render() {
    const {list, isUser} = this.state
    return (
      <View className='search-root'>
        <GzhAuth />
        {list.length > 0 && <View className='search-result'>
          <BigGoodsBox isUser={isUser} data={list} />
        </View>}
      </View>
    )
  }
}

export default CouponGoods as ComponentClass<PageOwnProps, PageState>
